namespace Demo1
{

public class DrawGeometry
    {
        public Game game;

        GraphicsDevice gdevice;

        public Texture2D pixel;
        public SpriteBatch spritebatch;

        Rectangle sourcerect = new Rectangle(0, 0, 1, 1);

        public DrawGeometry(Game game)
        {
            this.game = game;
            IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.GameServices.GetService(typeof(IGraphicsDeviceService));
            gdevice = graphicsService.GraphicsDevice;
            spritebatch = new SpriteBatch(gdevice);
        }

        // create a 1x1 pixel texture with white color for optimal tint 
        private void createPixelTexture()
        {
            pixel = new Texture2D(gdevice, 1, 1, 1, ResourceUsage.RenderTarget, SurfaceFormat.Color, ResourcePool.Default);
            Surface surf = pixel.GetSurfaceLevel(0);
            Surface oldRenderTarget = gdevice.GetRenderTarget(0);
            gdevice.SetRenderTarget(0, surf);
            gdevice.Clear(Color.White);
            gdevice.SetRenderTarget(0, oldRenderTarget);
        }

        public void drawPoint(Vector2 point, Color color)
        {
            spritebatch.Begin();
            spritebatch.Draw(pixel, point, color);
            spritebatch.End();
        }
        
        public void drawLine(Vector2 p1, Vector2 p2, Color color)
        {
            if (pixel == null)
                createPixelTexture();

            float angle = 0f;
            Vector2 diff = p2 - p1;   

            // calculate rotation angle
            angle = (float)Math.Acos((p2.X - p1.X) / diff.Length());
            // build destrect
            Rectangle destrect = new Rectangle((int)p1.X, (int)p1.Y, (int)diff.Length(), 1);

            spritebatch.Begin();
            spritebatch.Draw(pixel, destrect, sourcerect, color, angle, Vector2.Zero, SpriteEffects.None, 0);
            spritebatch.End();
        }

        public void drawRectangle(Rectangle rectangle, Color color)
        {
            Vector2 TopLeft = new Vector2(rectangle.Left, rectangle.Top);
            Vector2 TopRight = new Vector2(rectangle.Right, rectangle.Top);
            Vector2 BottomLeft = new Vector2(rectangle.Left, rectangle.Bottom);
            Vector2 BottomRight = new Vector2(rectangle.Right, rectangle.Bottom);

            drawLine(TopLeft, TopRight, color);
            drawLine(TopLeft, BottomLeft, color);
            drawLine(TopRight, BottomRight, color);
            drawLine(BottomLeft, BottomRight, color);
        }

        public void filledRectangle(Rectangle rectangle,Color color)
        {
            if (pixel == null)
                createPixelTexture();

            spritebatch.Begin();
            spritebatch.Draw(pixel, rectangle, color);
            spritebatch.End();
        }
}